package com.sum.util;

import cn.hutool.core.util.ReUtil;
import com.sum.entry.Cell;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;

import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

/**
 * CellUtil
 *
 * @author shuzh
 */
public class CellUtil {


    /**
     * 基于字母获取对应的列
     *
     * @param colName AB
     * @return
     */
    public static int getColIndexByName(String colName) {
        int sum = 0;
        colName = colName.toUpperCase();
        for (int i = 0; i < colName.length(); ++i) {
            char c = colName.charAt(i);
            int num = c - 64;
            sum += (int)Math.pow(26, colName.length() - 1 - i) * num;
        }
        return sum;
    }


    public static String getColNameByIndex(int colIndex) {
        String result = StringUtils.EMPTY;
        while (colIndex > 0) {
            colIndex--;
            int num = colIndex % 26;
            result = String.format("%c%s", num + 65, result);
            colIndex /= 26;
        }

        return result.toUpperCase();
    }


    /**
     * 基于单元格定位转换为数字定位
     *
     * @param cellName A6
     * @return
     */
    @SneakyThrows
    public static Cell getCell(String cellName) {
        // 获取列
        String colName = ReUtil.get("[a-zA-Z]+", cellName, 0);
        Integer rowIndex = ReUtil.getFirstNumber(cellName);
        if (StringUtils.isEmpty(colName) || null == rowIndex) {
            throw new Exception(cellName + "不符合规范，请检查");
        }

        int colIndex = getColIndexByName(colName);
        return new Cell(colIndex, rowIndex);
    }

    /**
     * 基于两个单元格，获取两者之间所有单元格
     *
     * @param begin
     * @param end
     * @return
     */
    public static Set<Cell> getCellSet(Cell begin, Cell end) {
        int minCol = Math.min(begin.getColIndex(), end.getColIndex());
        int maxCol = Math.max(begin.getColIndex(), end.getColIndex());
        int minRow = Math.min(begin.getRowIndex(), end.getRowIndex());
        int maxRow = Math.max(begin.getRowIndex(), end.getRowIndex());

        Set<Cell> cellSet = new HashSet<>();
        for (int i = minCol; i <= maxCol; ++i) {
            for (int j = minRow; j <= maxRow; ++j) {
                Cell cell = new Cell(i, j);
                cellSet.add(cell);
            }
        }
        return cellSet;
    }


    public static Set<Cell> getCellSet(String configStr) {
        Set<Cell> cellSet = new TreeSet<>((c1, c2) ->
                c1.getColIndex() != c2.getColIndex() ?
                        Integer.compare(c1.getColIndex(), c2.getColIndex()) :
                        Integer.compare(c1.getRowIndex(), c2.getRowIndex()));

        String[] arr1 = configStr.split("[,，]");
        for (String str : arr1) {
            String[] arr2 = str.split("[:：]");
            if (arr2.length > 1) {
                Cell cell1 = CellUtil.getCell(arr2[0]);
                Cell cell2 = CellUtil.getCell(arr2[1]);
                Set<Cell> tmpSet = CellUtil.getCellSet(cell1, cell2);
                cellSet.addAll(tmpSet);
            } else {
                Cell cell = CellUtil.getCell(arr2[0]);
                cellSet.add(cell);
            }
        }
        return cellSet;
    }

}
